package org.example.cloudalibaba.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.cloudalibaba.dao.OrderDao;
import org.example.cloudalibaba.entity.Order;
import org.example.cloudalibaba.service.OrderService;
import org.example.cloudalibaba.service.feign.AccountService;
import org.example.cloudalibaba.service.feign.StorageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    public void create(Order order) {
        log.info("开始新建订单");
        if (orderDao.create(order) > 0) {
            // 1. 减少库存
            log.info("开始减少库存");
            storageService.deduct(order.getProductId(), order.getCount());
            // 2. 减少余额
            log.info("开始减少余额");
            accountService.deduct(order.getUserId(), order.getMoney());
            log.info("订单创建成功");

            log.info("修改订单状态");
            orderDao.update(order.getId(), 1);
        } else {
            log.info("订单创建失败");
        }
    }
}
